<?php

require_once ('BaseAjaxModule.php');

class UserModule extends BaseAjaxModule {
	
	public function checkLoginTask()	{
		$user = $this->getUser();
		if ($user == null)
			$this->view->result = array('login'=>0);
		else
			$this->view->result = array('login'=>1, 'id'=>$user->id);
	}
	
	public function editInfoTask()	{
		$user = $this->getUser();
		$user->fullname = $this->getParam('fullname');
		$user->gender = $this->getParam('gender');
		$user->hobbies = $this->getParam('hobbies');
		$user->dob_day = $this->getParam('dob_day');
		$user->dob_month = $this->getParam('dob_month');
		$user->dob_year = $this->getParam('dob_year');
		$this->view->result = $user->editInfo();
	}
	
	public function viewInfoTask()	{
		$id = $this->getParam('id');
		if ($id == null)	{
			$user = $this->getUser();
			if ($user == null)	{
				$this->view->setAjaxResponse($this->createErrorResult('Chưa đăng nhập!'));
				return;
			}
		} else {
			$user = new Model_User();
			$user->id = $id;
			if (!$user->connect())	{
				$this->view->setAjaxResponse($this->createErrorResult('Người dùng không tồn tại!'));
				return;
			}
		}
		
		$this->view->result = $user->getInfo();
	}
	
	public function registerTask()	{
		$user = new Model_User();
		$user->email = $this->getParam('email');
		$user->pwd = $this->getParam('pwd');
		$user->fullname = $this->getParam('fullname');
		$user->alias = $this->getParam('alias');
		try {
			$result = $user->register();
		} catch (Exception $ex)	{
			$this->view->setAjaxResponse($this->createErrorResult($ex->getMessage()));
			return;
		}
		if ($result == true)	{
			$this->view->result = 'success';
			//session
			$session = Joob_Session_Standard::getInstance();
			$session->setBehaviour(new Joob_Session_Behaviour_Namespace(null, 'Joob:Auth'));
			$session->set('id', $user->id);
		} else {
			$this->view->setAjaxResponse($this->createErrorResult($result));
		}
	}
	
	public function logoutTask()	{
		//destroy session
		$session = Joob_Session_Standard::getInstance();
		$session->setBehaviour(new Joob_Session_Behaviour_Namespace(null, 'Joob:Auth'));
		$session->set('id', null);
		$session->destroy();
		
		//invalidate cookie
		Model_PersistentLogin::createExpiredCookies ();
		
		$this->view->result = 'success';
	}

	public function loginTask()	{
		if (!$this->getRequest()->hasParam('email') || !$this->getRequest()->hasParam('pwd'))	{
			$this->view->setAjaxResponse($this->createErrorResult('Chưa nhập email hoặc mật khẩu'));
			return;
		}
		
		$email = $this->getParam('email');
		
		//FIXME: password should be filtered as well?
		$passwd = $this->getParam('pwd', '', array(), true);
		
		$user = new Model_User();
		$user->email = $email;
		$user->pwd = $passwd;
		$persistent = $this->getParam('persistent');
		
		$result = $user->login();
		if ($result === true)	{
			$this->view->result = array('id'=>$user->id);
			
			//session
			$session = Joob_Session_Standard::getInstance();
			$session->setBehaviour(new Joob_Session_Behaviour_Namespace(null, 'Joob:Auth'));
			$session->set('id', $user->id);
			
			if ($persistent == 1)	{
				$cookieModel = new Model_PersistentLogin();
				$cookieModel->generateCookie($user->id);
				//store the cookie for 30 days
				$time = time()+30*24*3600;
				setcookie('persistent_userID', $user->id, $time);
				setcookie('persistent_loginID', $cookieModel->loginID, $time);
			}
		} else {
			$this->view->setAjaxResponse($this->createErrorResult($result));
		}
	}
	public function saveLocationTask(){
		$user = $this->getUser();
		if ($user == null){
			return;
		}
		else if (!$this->getRequest()->hasParam('lat') || !$this->getRequest()->hasParam('lng'))	{
			return;
		}
		else{
			$user->saveLocation($this->getParam('lat'), $this->getParam('lng'));
			$this->view->result = 'success';
		}
	}
	public function getNearPlacesTask(){
		if (!$this->getRequest()->hasParam('lat') || !$this->getRequest()->hasParam('lng'))	{
			$this->view->setAjaxResponse($this->createErrorResult("undefined"));
		}
		else{
			$user = new Model_User();
			$lat = $this->getParam('lat');
			$lng = $this->getParam('lng');
			$this->view->result = $user->getNearbyPlace(0,$lat,$lng,5,0,40);
		}
	}
	
	public function getNearFriendsTask(){
		$user = $this->getUser();
		if ($user == null){
			$this->view->setAjaxResponse($this->createErrorResult("Chưa đăng nhập!"));
		}
		else{
			if (!$this->getRequest()->hasParam('lat') || !$this->getRequest()->hasParam('lng'))	{
				$this->view->setAjaxResponse($this->createErrorResult("undefined"));
			}
			else{
				$lat = $this->getParam('lat');
				$lng = $this->getParam('lng');
				$this->view->result = $user->getFriendsNearby($lat,$lng,5,0,100);
			}
		}
	}
	
	public function getNearUsersTask(){
		if (!$this->getRequest()->hasParam('lat') || !$this->getRequest()->hasParam('lng'))	{
			$this->view->setAjaxResponse($this->createErrorResult("undefined"));
		}
		else{
			$user = $this->getUser();
			if ($user == null){
				$user = new Model_User();
			}
			$lat = $this->getParam('lat');
			$lng = $this->getParam('lng');
			$this->view->result = $user->getUsersNearby($lat,$lng,5,0,100);
		}
	}
	
	public function checkinTask(){
		$user = $this->getUser();
		if ($user == null){
			$this->view->setAjaxResponse($this->createErrorResult("Chưa đăng nhập!"));
		}
		else{
			if (!$this->getRequest()->hasParam('place_id'))	{
				$this->view->setAjaxResponse($this->createErrorResult("place id is undefined"));
			}
			else{
				$place = new Model_Place();
				$place->id = $this->getParam('place_id');
				if($place->connect()){
					$this->view->result = $user->checkin($place->id);
				}
				else{
					$this->view->setAjaxResponse($this->createErrorResult("place is undefined"));
				}
			}
		}
	}
	
	public function checkoutTask(){
		$user = $this->getUser();
		if ($user == null){
			$this->view->setAjaxResponse($this->createErrorResult("Chưa đăng nhập!"));
		}
		else{
			if (!$this->getRequest()->hasParam('place_id'))	{
				$this->view->setAjaxResponse($this->createErrorResult("place id is undefined"));
			}
			else{
				$place = new Model_Place();
				$place->id = $this->getParam('place_id');
				if($place->connect()){
					$this->view->result = $user->checkout($place->id);
				}
				else{
					$this->view->setAjaxResponse($this->createErrorResult("place is undefined"));
				}
			}
		}
	}
	
	public function getNewNotificationsTask()	{
		$user = $this->getUser();
		$since = $user->last_notification_time;
		$user->updateNotificationTime();
		
		$model = new Model_Notification();
		$this->view->result = $model->getNewNotifications($user->id, $since);
	}
	
	public function getOldNotificationsTask()	{
		$user = $this->getUser();
		$since = $user->last_notification_time;
		
		$model = new Model_Notification();
		$this->view->result = $model->getOldNotifications($user->id, $since);
	}
	
	public function updateNotificationTimeTask()	{
		$user = $this->getUser();
		$user->since = time();
		$this->view->result = $user->edit('since');
	}
	
	public function countNewNotificationsTask()	{
		$user = $this->getUser();
		$since = $user->last_notification_time;
		
		$model = new Model_Notification();
		$this->view->result = $model->countNewNotifications($user->id, $since);
	}
}

?>